package com.fwmagic.spark.core.action

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
  * takeOrdered和top类似，只不过以和top相反的顺序返回元素。
  * top默认倒序，taskOrdered默认正序
  * top方法其实就是调用的taskOrdered，然后反转的结果
  */
object TakeOrderedDemo {
    def main(args: Array[String]): Unit = {
        val conf = new SparkConf()
                .setAppName(this.getClass.getSimpleName)
                .setMaster("local[*]")

        val sc = new SparkContext(conf)

        val rdd1: RDD[Int] = sc.makeRDD(1 to 10)

        //implicit val ruleRules = Ordering[Int].on[Int](-_)
        implicit val ruleRules = Ordering.by[Int, Int](t => -t)

        val array: Array[Int] = rdd1.takeOrdered(3)

        println(array.toBuffer)

        sc.stop()
    }
}
